package servlet;

import modle.Goods;
import modle.Order;
import service.GoodsService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "GoodsBuyServlet", value = "/goods_buy")
public class GoodsBuyServlet extends HttpServlet {

    private GoodsService goodsService = new GoodsService();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        Order o = null;

        if (request.getSession().getAttribute("order")!=null) {
            o = (Order) request.getSession().getAttribute("order");
        }else {
            o = new Order();
        }
        request.getSession().setAttribute("order",o);

        int goodsid = Integer.parseInt(request.getParameter("goodsid"));
        Goods goods = goodsService.getGoodsById(goodsid);

        if (goods.getStock()>0) {
            o.addGoods(goods);
            response.getWriter().print("ok");
        }else {
            response.getWriter().print("fail");
        }
    }
}
